package com.bookworms.helloandengine.game.ui;

import android.content.Intent;
import android.graphics.Color;
import org.anddev.andengine.entity.scene.Scene;
import org.anddev.andengine.entity.scene.background.ColorBackground;
import org.anddev.andengine.entity.scene.menu.MenuScene;
import org.anddev.andengine.entity.scene.menu.MenuScene.IOnMenuItemClickListener;
import org.anddev.andengine.entity.scene.menu.item.IMenuItem;
import org.anddev.andengine.entity.scene.menu.item.TextMenuItem;
import org.anddev.andengine.entity.scene.menu.item.decorator.ColorMenuItemDecorator;
import org.anddev.andengine.entity.sprite.Sprite;
import org.anddev.andengine.opengl.font.Font;
import org.anddev.andengine.opengl.font.FontFactory;
import org.anddev.andengine.opengl.texture.Texture;
import org.anddev.andengine.opengl.texture.TextureOptions;
import org.anddev.andengine.opengl.texture.region.TextureRegion;
import org.anddev.andengine.opengl.texture.region.TextureRegionFactory;
import test.test.R;


public class MainMenuScreen extends BaseScreen implements IOnMenuItemClickListener {

    private static final int MENU_NEW_GAME = 0;
    private static final int MENU_QUIT = 1;
    public static final int MENU_FONT_SIZE = 52;

    protected Scene mRootMenuScene;
    private Font mMenuFont;
    private TextureRegion mBackgroundTextureRegion;
    protected MenuScene mMenuItemsScene;

    @Override
    public void onLoadResources() {
        Texture menuBackgroundTexture = new Texture(128, 128, TextureOptions.BILINEAR_PREMULTIPLYALPHA);
        TextureRegionFactory.setAssetBasePath("gfx/");
        mBackgroundTextureRegion = TextureRegionFactory.createFromAsset(menuBackgroundTexture, this, "logo.png", 0, 0);
        mEngine.getTextureManager().loadTexture(menuBackgroundTexture);
        Texture fontTexture = new Texture(256, 256, TextureOptions.BILINEAR_PREMULTIPLYALPHA);
        FontFactory.setAssetBasePath("font/");
        mMenuFont = FontFactory.createFromAsset(fontTexture, this, "LiberationSans-Regular.ttf", MENU_FONT_SIZE, true, Color.WHITE);
        mEngine.getTextureManager().loadTexture(fontTexture);
        mEngine.getFontManager().loadFont(mMenuFont);
    }

    @Override
    public Scene onLoadScene() {
        mRootMenuScene = super.onLoadScene();
        mMenuItemsScene = createMenuScene();
        Sprite backgroundSprite = new Sprite(getLogoXPosition(), getLogoYPosition(), mBackgroundTextureRegion);
        mRootMenuScene.setBackground(new ColorBackground(1f, 1f, 1f));
        mRootMenuScene.setChildScene(mMenuItemsScene, false, true, true);
        mRootMenuScene.attachChild(backgroundSprite);
        return mRootMenuScene;
    }

    private int getLogoYPosition() {
        return 10;
    }

    private int getLogoXPosition() {
        return (mCameraWidth - mBackgroundTextureRegion.getWidth())/2;
    }

    private MenuScene createMenuScene() {
        MenuScene menuScene = new MenuScene(mCamera);
        menuScene.setPosition(0, getMenuYPosition());
        IMenuItem newGameItem = new ColorMenuItemDecorator(new TextMenuItem(MENU_NEW_GAME, mMenuFont, getString(R.string.menu_item_play)), 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f);
        setFadeOutBlend(newGameItem);
        menuScene.addMenuItem(newGameItem);
        IMenuItem quitItem = new ColorMenuItemDecorator(new TextMenuItem(MENU_QUIT, mMenuFont, getString(R.string.menu_item_exit)), 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f);
        setFadeOutBlend(quitItem);
        menuScene.addMenuItem(quitItem);
        menuScene.buildAnimations();
        menuScene.setBackgroundEnabled(false);
        menuScene.setOnMenuItemClickListener(this);
        return menuScene;
    }

    private int getMenuYPosition() {
        return mCameraHeight / 2 - 2 * MENU_FONT_SIZE;
    }

    @Override
    public boolean onMenuItemClicked(final MenuScene scene, final IMenuItem item, final float itemX, final float itemY) {
        switch (item.getID()) {
            case MENU_NEW_GAME:
                Intent newGameIntent = new Intent(this, GameScreen.class);
                startActivity(newGameIntent);
                return true;
            case MENU_QUIT:
                finish();
                return true;
            default:
                return false;
        }
    }
}